﻿using Glue.Converters;

namespace Glue.Internals
{
    internal partial class RelationFactory
    {
        private class Relation : IRelation
        {
            private readonly IConverter converter;
            public IObjectMember LeftMember { get; private set; }
            public IObjectMember RightMember { get; private set; }
            public RelationDirection Direction { get; private set; }

            /// <ensure>direction != None</ensure>
            public Relation(IObjectMember left, IObjectMember right, IConverter converter, RelationDirection direction)
            {
                if (direction == RelationDirection.None)
                    throw new GlueException("The relation cannot have Direction==None. Relation ('" + left.Name + "' and '" + right.Name + "')");
                this.converter = converter;
                LeftMember = left;
                RightMember = right;
                Direction = direction;
            }

            public IConverter GetConverter()
            {
                return converter;
            }

            public bool HasTowardsLeft()
            {
                return Direction == RelationDirection.TowardsLeft || Direction == RelationDirection.TwoWays;
            }

            public bool HasTowardsRight()
            {
                return Direction == RelationDirection.TowardsRight || Direction == RelationDirection.TwoWays;
            }

            public bool CanMapFromThisLeftSideObject(object thisObject)
            {
                return LeftMember.CanMapFrom(thisObject);
            }

            public bool CanMapFromThisRightSideObject(object thisObject)
            {
                return RightMember.CanMapFrom(thisObject);
            }
        }
    }
}